package com.lsa.helpers;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

public class SubListAdapter<T> implements List<T>
{
	List<T> list;
	int from;
	int to;

	public SubListAdapter(List<T> list, int from, int to)
	{
		this.list = list;
		this.from = from;
		this.to = to;
	}
	
	public static SubListAdapter adapt(List list, int from, int to)
	{
		return new SubListAdapter(list, from ,to);
	}

	@Override
	public boolean add(T e)
	{
		throw new NotImplementedException();
	}

	@Override
	public void add(int index, T element)
	{
		throw new NotImplementedException();
	}

	@Override
	public boolean addAll(Collection<? extends T> c)
	{
		throw new NotImplementedException();
	}

	@Override
	public boolean addAll(int index, Collection<? extends T> c)
	{
		throw new NotImplementedException();
	}

	@Override
	public void clear()
	{
		throw new NotImplementedException();
	}

	@Override
	public boolean contains(Object o)
	{
		return this.indexOf(o) > -1;
	}

	@Override
	public boolean containsAll(Collection<?> c)
	{
		throw new NotImplementedException();
	}

	@Override
	public T get(int index)
	{
		if (this.from + index > this.to)
		{
			throw new ArrayIndexOutOfBoundsException();
		}

		return this.list.get(this.from + index);
	}

	@Override
	public int indexOf(Object o)
	{
		if (o == null)
		{
			for (int i = this.from; i < this.to; i++)
				if (this.list.get(i) == null)
					return i;
		}
		else
		{
			for (int i = this.from; i < this.to; i++)
				if (o.equals(this.list.get(i)))
					return i;
		}
		return -1;
	}
	
	public int lastIndexOf(Object o)
	{
		if (o == null)
		{
			for (int i = this.to - 1; i > this.from - 1; i--)
				if (this.list.get(i) == null)
					return i;
		}
		else
		{
			for (int i = this.to - 1; i > this.from - 1; i--)
				if (o.equals(this.list.get(i)))
					return i;
		}
		return -1;
	}

	//
	@Override
	public boolean isEmpty()
	{
		return this.list.isEmpty();
	}

	@Override
	public Iterator<T> iterator()
	{
		throw new NotImplementedException();
	}

	@Override
	public ListIterator<T> listIterator()
	{
		throw new NotImplementedException();
	}

	@Override
	public ListIterator<T> listIterator(int index)
	{
		throw new NotImplementedException();
	}

	@Override
	public boolean remove(Object o)
	{
		throw new NotImplementedException();
	}

	@Override
	public T remove(int index)
	{
		throw new NotImplementedException();
	}

	@Override
	public boolean removeAll(Collection<?> c)
	{
		throw new NotImplementedException();
	}

	@Override
	public boolean retainAll(Collection<?> c)
	{
		throw new NotImplementedException();
	}

	@Override
	public T set(int index, T element)
	{
		if (this.from + index > this.to)
		{
			throw new ArrayIndexOutOfBoundsException();
		}

		return this.list.set(this.from + index, element);
	}

	@Override
	public int size()
	{
		return this.to - this.from + 1;
	}

	@Override
	public List<T> subList(int fromIndex, int toIndex)
	{
		throw new ArrayIndexOutOfBoundsException();
	}

	@Override
	public Object[] toArray()
	{
		Object[] array = new Object[this.size()];
		
		for (int i = 0; i < this.size(); i++)
		{
			array[i] = this.get(i);
		}
		
		return array;
	}

	@Override
	public <T> T[] toArray(T[] a)
	{
		throw new ArrayIndexOutOfBoundsException();
	}
}
